package top.continew.admin.controller.pdnew;

import cn.hutool.core.bean.BeanUtil;
import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.rpc.base.TransResult;
import cn.shengchao.examstar.rpc.product.api.IProductCourseSettingRpc;
import cn.shengchao.examstar.rpc.product.req.ProductCourseSettingBatchSaveDto;
import cn.shengchao.examstar.rpc.product.req.ProductCourseSettingDto;
import cn.shengchao.examstar.rpc.product.req.ProductCourseSettingPageQuery;
import cn.shengchao.examstar.rpc.product.req.ProductCourseSettingQuery;
import cn.shengchao.examstar.rpc.product.res.ProductCourseSettingHasListVo;
import cn.shengchao.examstar.rpc.product.res.ProductCourseSettingVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.req.pdnew.ProductCourseSettingCourseLessonSaveDto;
import top.continew.admin.req.pdnew.ProductCourseSettingGkSaveDto;
import top.continew.admin.req.pdnew.ProductCourseSettingLessonSaveDto;
import top.continew.starter.web.model.R;

/**
 * @program: examstar-manger-api
 * @description: 课程产品合作有效期
 * @author: Xiong weiqiang
 * @create: 2025-06-30 17:54
 */
@Tag(name = "课程产品合作有效期")
@RestController
@RequiredArgsConstructor
@RequestMapping("/productCourseSetting")
public class ProductCourseSettingController {
    @DubboReference
    private IProductCourseSettingRpc productCourseSettingRpc;

    @Operation(summary = "课程产品合作有效期", description = "课程产品合作有效期")
    @GetMapping("/pageQuery")
    PageResult<ProductCourseSettingVo> pageQueryByUserId(ProductCourseSettingPageQuery pageQuery) {
        if (pageQuery==null){
            pageQuery=new ProductCourseSettingPageQuery();
            pageQuery.setPage(1);
            pageQuery.setSize(10);
        }
        return productCourseSettingRpc.queryProductCourseSetting(pageQuery);
    }

    @Operation(summary = "课程产品合作有效期按课程、章节、课时展示", description = "课程产品合作有效期按课程、章节、课时展示")
    @GetMapping("/showByProductId")
    public R<ProductCourseSettingHasListVo> showByProductId(ProductCourseSettingQuery query) {
        return R.ok(productCourseSettingRpc.queryProductCourseSettingForShow(query));
    }

    @Operation(summary = "新增一个课程产品合作有效期", description = "新增单个课程产品合作有效期")
    @PostMapping("/addCorpProductSetting")
    public R<String> addCorpProductSetting(@Validated @RequestBody ProductCourseSettingLessonSaveDto productCourseSettingSaveDto) {
        ProductCourseSettingDto productCourseSettingDto=new ProductCourseSettingDto();
        BeanUtil.copyProperties(productCourseSettingSaveDto,productCourseSettingDto);
        TransResult<String> transResult=productCourseSettingRpc.saveProductCourseSetting(productCourseSettingDto);
        if (transResult.getSuccess()){
            return R.ok(transResult.getMessage());
        }else {
            return R.fail(R.fail().getCode(), transResult.getMessage());
        }
    }
    @Operation(summary = "按课程章节新增产品合作有效期", description = "按课程章节新增产品合作有效期")
    @PostMapping("/addCourseProductSetting")
    public R<String> addCourseProductSetting(@Validated @RequestBody ProductCourseSettingCourseLessonSaveDto productCourseSettingSaveDto) {
        ProductCourseSettingBatchSaveDto productCourseSettingBatchSaveDto=new ProductCourseSettingBatchSaveDto();
        BeanUtil.copyProperties(productCourseSettingSaveDto,productCourseSettingBatchSaveDto);
        TransResult<String> transResult=productCourseSettingRpc.saveProductCourseSettingByCourse(productCourseSettingBatchSaveDto);
        if (transResult.getSuccess()){
            return R.ok(transResult.getMessage());
        }else {
            return R.fail(R.fail().getCode(), transResult.getMessage());
        }
    }

    @Operation(summary = "新增整个赶考课程产品合作有效期", description = "新增整个赶考课程产品合作有效期")
    @PostMapping("/addGkProductSetting")
    public R<String> addGkProductSetting(@Validated @RequestBody ProductCourseSettingGkSaveDto productCourseSettingSaveDto) {
        ProductCourseSettingBatchSaveDto productCourseSettingBatchSaveDto=new ProductCourseSettingBatchSaveDto();
        BeanUtil.copyProperties(productCourseSettingSaveDto,productCourseSettingBatchSaveDto);
        TransResult<String> transResult=productCourseSettingRpc.saveProductCourseSettingForGkCourse(productCourseSettingBatchSaveDto);
        if (transResult.getSuccess()){
            return R.ok(transResult.getMessage());
        }else {
            return R.fail(R.fail().getCode(), transResult.getMessage());
        }
    }
}